package io.focus.mijin.zuul.ratelimit.support;

import io.focus.mijin.zuul.ratelimit.config.properties.RateLimitProperties.Policy.MatchType;
import io.focus.mijin.zuul.ratelimit.config.properties.RateLimitProperties.Policy.Type;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.convert.converter.Converter;

/**
 * @author Liel Chayoun
 */
public class StringToMatchTypeConverter implements Converter<String, MatchType> {

    private static final String DELIMITER = "=";

    @Override
    public MatchType convert(String type) {
        if (type.contains(DELIMITER)) {
            String[] matchType = type.split(DELIMITER);
            return new MatchType(Type.valueOf(StringUtils.trim(matchType[0].toUpperCase())), StringUtils.trim(matchType[1]));
        }
        return new MatchType(Type.valueOf(StringUtils.trim(type.toUpperCase())), null);
    }
}
